package com.jxt.mina.client;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import com.jxt.mina.tools.CodecFactory;


public class ClientTest1 {

	private static Logger logger = Logger.getLogger(ClientTest1.class);
	private static final String HOST = "127.0.0.1";
	private static final int PORT = 8888;

	public static void main(String[] args) {
		IoConnector connector = new NioSocketConnector();
		connector.setConnectTimeout(30000);
		connector.getFilterChain().addLast(
				"codec",
				new ProtocolCodecFilter(new CodecFactory()));
		connector.setHandler(new ClientHandler());
		IoSession session = null;
		ConnectFuture connect = null;
		try {
			connect = connector.connect(new InetSocketAddress(
					HOST, PORT));
			connect.awaitUninterruptibly();
			session = connect.getSession();
//			String flag = UUID.randomUUID().toString();
//			session.setAttribute("userId", flag);
			List list = new ArrayList();
			list.add("402880d83a528775013a52e8b9fb0001");
			JSONObject json = new JSONObject();
			json.accumulate("listId", list);
			json.accumulate("message", "靠靠靠");
			json.accumulate("sendTime", "2012-10-29 11:42");
			json.accumulate("memberId", "4028801939e1e0030139e1e024be0001");
			session.write(json);
		} catch (Exception e) {
			logger.error("服务端异常...",e);
		}finally{
			session.getCloseFuture().awaitUninterruptibly();
			connector.dispose();
		}	
	}
}
